# -*- coding: utf-8 -*-
"""
@Time    : 2024/6/25 10:05 
@Author  : ZhangShenao 
@File    : app_service.py 
@Desc    : AI应用服务
"""
import uuid
from dataclasses import dataclass

from injector import inject

from internal.model import App
from pkg.sqlalchemy import SQLAlchemy


@inject
@dataclass
class AppService:
    """
    AI应用服务
    """
    db: SQLAlchemy  # 依赖注入数据库DB

    def create(self) -> App:
        """
        创建AI应用
        """

        with self.db.auto_commit():
            # 创建新的APP实体对象
            app = App(account_id=uuid.uuid4(),
                      name='测试AI应用1',
                      icon='',
                      description='这是测试AI用1')
            # 将实体对象添加到数据库会话中
            self.db.session.add(app)

        # 返回新创建的APP实体对象
        return app

    def get_by_id(self, app_id: uuid.UUID) -> App:
        """
        根据ID查询AI应用
        """
        return self.db.session.query(App).get(app_id)

    def get_all(self) -> [App]:
        """
        查询所有AI应用
        """
        return self.db.session.query(App).all()

    def update(self, app_id: uuid.UUID) -> App:
        """更新AI应用"""
        with self.db.auto_commit():
            app = self.get_by_id(app_id)
            app.name = '测试AI应用111'

        return app

    def delete(self, app_id: uuid.UUID) -> App:
        """删除AI应用"""
        with self.db.auto_commit():
            app = self.get_by_id(app_id)
            self.db.session.delete(app)

        return app
